package com.hupeng.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hupeng.VO.FoodVO;
import com.hupeng.VO.ProductVO;
import com.hupeng.VO.ResultVO;
import com.hupeng.domain.ProductCategory;
import com.hupeng.domain.ProductInfo;
import com.hupeng.serivce.impl.CategoryServiceImpl;
import com.hupeng.serivce.impl.ProductServiceImpl;
import com.hupeng.utils.ResultVOUtil;

@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {
	@Autowired
	private CategoryServiceImpl cateService;
	@Autowired
	private ProductServiceImpl productService;
	
	@GetMapping("/list")
	public ResultVO<Object> list(){
//		ResultVO<Object> resultVO = new ResultVO<>();
		//查询类目
		List<ProductCategory> categoryAll = cateService.findAll();
		//查询上架商品
		List<ProductInfo> findUpAll = productService.findUpAll();
		
		//创建类目List
		List<ProductVO> productVOList = new ArrayList<ProductVO>();
		//创建商品类目
		
		for (ProductCategory category : categoryAll) {
			ProductVO productVO = new ProductVO();
			Integer categoryType = category.getCategoryType();
			productVO.setCategoryName(category.getCategoryName());
			productVO.setCategoryType(categoryType);
			List<FoodVO> foodList = new ArrayList<FoodVO>();
			for (ProductInfo productInfo : findUpAll) {
				if(productInfo.getCategoryType() == categoryType){
					FoodVO foodVO = new FoodVO();
					foodVO.setProductId(productInfo.getProductId());
					foodVO.setProductName(productInfo.getProductName());
					foodVO.setProductIcon(productInfo.getProductIcon());
					foodVO.setProductPrice(productInfo.getProductPrice());
					foodVO.setProductDescription(productInfo.getProductDescription());
					foodList.add(foodVO);
				}
			}
			productVO.setFoodsList(foodList);
			productVOList.add(productVO);
		}
		
	
		return ResultVOUtil.success(productVOList);
	}
}
